package admin

import (
	"blogApi/controllers"
	. "blogApi/models"
	"strings"
	"time"

	"github.com/astaxie/beego"
)

type CommentController struct {
	controllers.BaseController
}

type Result struct {
	Id           int       `json:"id"`
	UserId       int       `json:"userId"`
	ArticleId    int       `json:"articleId"`
	Type         int       `json:"type"`
	Pid          int       `json:"pid"`
	OriginId     int       `json:"originId"`
	UserName     string    `json:"userName"`
	ArticleTitle string    `json:"articleTitle"`
	Content      string    `json:"content"`
	Status       int       `json:"status"`
	Cai          int       `json:"cai"`
	Zan          int       `json:"zan"`
	CreatedAt    time.Time `json:"createdAt"`
	UpdatedAt    time.Time `json:"updatedAt"`
}

// Index 获取列表
func (cont *CommentController) Index() {
	page, pageErr := cont.GetInt("page")
	status, statusErr := cont.GetInt("status")
	limit, limitErr := cont.GetInt("limit")
	if pageErr != nil {
		page = 1
	}
	if limitErr != nil {
		limit = 10
	}
	// 表前缀
	db_prefix := beego.AppConfig.String("db.prefix")
	query := Db.Model(&Comment{}).Joins("left join " + db_prefix + "users on " + db_prefix + "comments.user_id = " + db_prefix + "users.id").Joins("left join " + db_prefix + "articles on " + db_prefix + "comments.article_id = " + db_prefix + "articles.id")
	//查询条件
	if keywords := strings.TrimSpace(cont.GetString("keywords")); keywords != "" {
		query.Where(Db.Where(db_prefix+"comments.content LIKE ?", "%"+keywords+"%").Or(db_prefix+"articles.title LIKE ?", "%"+keywords+"%"))
	}
	if statusErr == nil {
		query.Where(db_prefix+"comments.status = ?", status)
	}
	offset := (page - 1) * limit
	// 总数
	var total int64
	// 返回数据
	list := make([]*Result, 0)
	query.Count(&total)
	query.Debug().Select(db_prefix + "users.name as user_name," + db_prefix + "articles.title as article_title," + db_prefix + "comments.*").Order("id desc").Offset(offset).Limit(limit).Find(&list)
	resMap := make(map[string]interface{})
	resMap["list"] = list
	resMap["total"] = total
	cont.ResJson(0, "获取成功", resMap, nil)
}

/**
*删除
 */
func (cont *CommentController) Destroy() {
	if cont.Ctx.Input.IsDelete() == false {
		cont.ResJson(1, "请求方式错误", nil, nil)
	}
	id := cont.Input().Get("id")
	var input Comment
	if result := Db.Debug().Where("id = ?", id).Delete(&Comment{}); result.Error != nil {
		cont.ResJson(1, "操作失败", result.Error, nil)
	}
	cont.ResJson(0, "操作成功", input, nil)
}

/**
* 更新
**/
func (cont *CommentController) Update() {
	if cont.Ctx.Input.IsPut() == false {
		cont.ResJson(1, "请求方式错误", nil, nil)
	}
	var input Comment
	// 接收参数
	if err := cont.ParseForm(&input); err != nil {
		cont.ResJson(1, "参数解析失败", nil, nil)
	}
	// 验证
	if msg := input.SelfValid(); msg != nil {
		cont.ResJson(1, msg, input, nil)
	}
	if result := Db.Model(&Comment{}).Where("id = ?", input.Id).Select("Content", "Updated_at", "Status", "Cai", "Zan").Updates(input); result.Error != nil {
		cont.ResJson(1, "存入失败", result.Error, nil)
	}
	cont.ResJson(0, "操作成功", input, nil)
}

/**
* 替换
 */
func (cont *CommentController) Replace() {

}
